package com.sikao.app.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.bean.Bean;
import com.sikao.app.bean.ChangePayPswSubmitBean;
import com.sikao.app.dao.ChangePayPswModel;
import com.sikao.app.dao.RetrievePayPswModel;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.JSONUtil;
/**
 * 修改支付密码
 */
public class ChangePayPswActivity extends BaseActivity implements View.OnClickListener {

	private TextView titleTv;
	private ImageView backIv;
	private EditText originalPswEt;
	private EditText newPswEt1;
	private EditText newPswEt2;
	private Button changePswBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_change_pay_psw);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initData() {

	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		backIv = (ImageView) findViewById(R.id.title_left_img);
		originalPswEt = (EditText) findViewById(R.id.original_psw_et);
		newPswEt1 = (EditText) findViewById(R.id.new_psw_et1);
		newPswEt2 = (EditText) findViewById(R.id.new_psw_et2);
		changePswBtn = (Button) findViewById(R.id.change_psw_btn);
	}

	@Override
	protected void initListener() {
		backIv.setOnClickListener(this);
		changePswBtn.setOnClickListener(this);
	}

	@Override
	protected void init() {
		titleTv.setText("修改支付密码");
		backIv.setVisibility(View.VISIBLE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_img:
				finish();
				break;
			case R.id.change_psw_btn:
				changePsw();
				break;
			default:
				break;
		}
	}

	private void changePsw() {

		if (!checkInput()) {
			return;
		}
		ChangePayPswSubmitBean bean = new ChangePayPswSubmitBean();
		bean.setOldPayPwd(originalPswEt.getText().toString());
		bean.setPayPwd(newPswEt1.getText().toString());
		bean.setAgapwd(newPswEt2.getText().toString());
		new ChangePayPswModel(this, new OnNetBackListener<Bean>() {
			@Override
			public void netBack(Bean data) {
				if (data == null){
					return;
				}
				if (data.getStatus().equals("1")){
					Toast.makeText(ChangePayPswActivity.this, "成功修改支付密码", Toast.LENGTH_SHORT).show();
					finish();
				}else{
					Toast.makeText(ChangePayPswActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
				}
			}
		}).request(JSONUtil.toJSON(bean), true);
	}

	private boolean checkInput() {

		if (TextUtils.isEmpty(originalPswEt.getText().toString())) {
			Toast.makeText(this, "请输入原密码", Toast.LENGTH_SHORT).show();
			return false;
		}

		if (newPswEt1.getText().toString().length() != 6) {
			Toast.makeText(this, "请输入新六位数字密码", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (originalPswEt.getText().toString().equals(newPswEt1.getText().toString())) {
			Toast.makeText(this, "新密码和原密码相同", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (TextUtils.isEmpty(newPswEt2.getText().toString())) {
			Toast.makeText(this, "请再次输入新密码", Toast.LENGTH_SHORT).show();
			return false;
		}

		if (!(newPswEt1.getText().toString()).equals(newPswEt2.getText().toString())) {
			Toast.makeText(this, "两次新密码不一致", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

}
